#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "../head/sst.h"
#include "../head/bst.h"
#include "../head/FileOps.h"

template<class S, class I, template<class _S, class _I> class C>
void testBst(vector<string> words,const string &f_name, const string keyword, bool ordered);

void testBook(const string bookname, string keyword);


int main(){
	// testBook("bible.txt", "god");
	testBook("communist.txt", "unite");
	return 0;
}

template<class S, class I, template<class _S, class _I> class C>
void testBst(vector<string> words, const string &f_name, const string keyword, bool ordered){

	time_t startTime = clock();
	// BST<string, int> bst = BST<string, int>();
	C<S,I> bst;
	if(ordered)
		sort( words.begin() , words.end() );
	for(vector<string>::iterator iter = words.begin();
			iter != words.end();iter++){
		int *res = bst.search(*iter);
		if(res == NULL){
			bst.insert(*iter, 1);
		}else{
			(*res)++;
		}
	
	}
	cout << keyword << " : " << *bst.search(keyword) << endl;
	time_t endTime = clock();
	cout << f_name << ", time: " << double(endTime - startTime) / CLOCKS_PER_SEC 
		<< " s." << endl;
	cout << endl;
}

void testBook(const string bookname, string keyword){
	string filename = bookname;
	vector<string> words;
	if(FileOps::readFile(filename, words)){
		cout << "There are totally " << words.size() << 
			" words in " << filename  << endl;

		
		testBst<string,int,BST>(words, "BST", keyword, false);		
		testBst<string,int,BST>(words, "BST Ordered", keyword, true);		
		testBst<string,int,SST>(words, "SST", keyword, false);		
	}
}
